package cn.com.nes.site.module.dao;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.Page;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.net.URLDecoder;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Repository
public class ExpandDaoImpl extends BaseConnDaoImpl {
	
	public PageData getExpandList(Connection conn,DataReq req) {
		StringBuffer sb = new StringBuffer();
		StringBuffer sbfrom = new StringBuffer();
		List<Map<String, Object>> list = null ;
		PageData res = new PageData();
		try {
			sb.append(" select if(a.sfmr='1','是','否') sfmrName,a.sfmr,a.uuid,a.expendName,a.expendType,b.`name` expendTypeName,a.companyId,a.jsondata,a.createUid,a.createTime,a.modifyUid,a.modifyTime,a.isdelete " +
					" from t_expand a " +
					" LEFT JOIN t_param b ON a.expendType=b.value AND b.param_type='111' " +
					" where isdelete <>'1' ") ;
			if (!StringUtils.isEmpty(req.getUinfo().getCompanyId())){
				sb.append(" and a.companyId = '").append(req.getUinfo().getCompanyId()).append("' ") ;
			}
			if (!StringUtils.isEmpty((String)req.getReqData().get("uuid"))){
				sb.append(" and a.uuid = '").append((String)req.getReqData().get("uuid")).append("' ") ;
			}
			// System.out.println(sb);
			sbfrom.append(sb.toString());
			int idx = sbfrom.indexOf(" from ");
			sbfrom.delete(0, idx);
			Page page = new Page(conn,sbfrom.toString(), req.getPageid(), req.getPagesize(), res,new Object[] {});
			sb.append(" order by  a.createTime desc ");
			sb.append(" limit ").append(page.getOffsetlimit()).append(",").append(req.getPagesize());
			list = queryForList(conn,sb.toString(),new Object[] {});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		return res;
    }
	
	public PageData saveExpand(Connection conn,DataReq req) {
    	Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("参数信息保存失败");
		param.put("tableName", "t_expand");
		param.put("companyId", req.getUinfo().getCompanyId());
		try {
			String jsondata = (String) param.get("jsondata") ;
			jsondata =  URLDecoder.decode(jsondata, "UTF-8");
			param.put("jsondata", jsondata);
			//是否为小程序默认模板
			String sfmr=(String)param.get("sfmr");
			if("1".equals(sfmr)){
				//将公司其他模板是否默认改为0
				StringBuffer sb = new StringBuffer();
				sb.append(" select uuid from t_expand where expendType='").append(param.get("expendType")).append("' and companyId= '").append(req.getUinfo().getCompanyId()).append("' ") ;
				sb = DBUtils.sql_check(sb) ;
				List<Map<String, Object>> list = this.queryForList(conn,sb.toString(),new Object[] {});
				if(list.size()>0){
					sb.delete(0, sb.length());
					sb.append(" update t_expand set sfmr ='0' where uuid in ('null'");
					for (int i = 0; i < list.size(); i++) {
						sb.append(",'").append(list.get(i).get("uuid")).append("'");
					}
					sb.append(") ");
					this.update(conn, sb.toString(),new Object[] {});
				}
			}
			String uuid=(String)param.get("uuid");
			if (uuid ==null || "".equals(uuid)){
				res =  insertTableData(conn, req) ;
			}else{
				param.put("keys","uuid");
				res =  updateTableData(conn, req) ;
			}
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {
			
		}
		return res;
    }
	
	public PageData delExpand(Connection conn,DataReq req) {
    	Map<String, Object> param = req.getReqData();
		PageData res = new PageData();
		res.setRetCode(9);
		res.setRetMessage("删除失败");
		param.put("tableName", "t_expand");
		param.put("keys","uuid");
		param.put("isdelete", "1");
		try {
			res =  updateTableData(conn, req) ;
		} catch (Exception e) {
			res.setRetCode(9);
			res.setRetMessage("操作失败！");
			e.printStackTrace();
		} finally {
			
		}
		return res;
    }
	
	public PageData getExpandId(Connection conn,DataReq req) {
		StringBuffer sb = new StringBuffer();
		List<Map<String, Object>> list = null ;
		PageData res = new PageData();
		try {
			sb.append(" select * from t_expand a  where a.isdelete <>'1' ");
			if (!StringUtils.isEmpty(req.getUinfo().getCompanyId())){
				sb.append(" and a.companyId = '").append(req.getUinfo().getCompanyId()).append("' ") ;
			}
			if (!StringUtils.isEmpty((String)req.getReqData().get("uuid"))){
				sb.append(" and a.uuid = '").append((String)req.getReqData().get("uuid")).append("' ") ;
			}else{//传uuid为编辑查之前保存的模板，不传uuid为新增查该类型默认模板
				sb.append(" and a.sfmr='1' and a.expendType='").append(req.getReqData().get("expendType")).append("' ");
			}
			list = queryForList(conn,sb.toString(),new Object[] {});
			res.setPage(list);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		return res;
    }
	
}